package com.frame.work.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.frame.work.common.util.Time;
import com.frame.work.entity.TblSecurityLog;
import com.frame.work.entity.TimeCount;
import com.frame.work.entity.dto.TblSecurityLogDTO;
import com.frame.work.entity.dto.TimeCountDTO;
import com.frame.work.service.TblSecurityLogService;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * 报警消息日志
 *
 * @author hewenze
 * @since 2020-04-10 11:15:12
 */
@RestController
@RequestMapping("/api/security")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SecurityLogController extends ApiController {
    /**
     * 服务对象
     */


    @Autowired
    private TblSecurityLogService tblSecurityLogService;


    /**
     * 通过时间查询
     *
     * @return 单条数据
     */
    //@CheckLogin
    @GetMapping("/device")
    public R selectLog(@RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("deviceMac") String deviceMac,
                       @RequestParam("gatewayMac") String gatewayMac,
                       @RequestParam("start") long start,
                       @RequestParam("end") long end) {

        if (pageNum == 0) {
            pageNum = 0;
        } else {
            pageNum = (pageNum - 1) * pageSize;
        }
        pageSize = pageNum + pageSize;
        List<TblSecurityLog> tblSecurityLogs = tblSecurityLogService.selectByTime(pageNum, pageSize, gatewayMac, deviceMac, Time.stampToDate(start), Time.stampToDate(end));

        List<TblSecurityLogDTO> tblSecurityLogDTOS = new ArrayList<>();
        for (TblSecurityLog tblSecurityLog : tblSecurityLogs) {
            TblSecurityLogDTO tblSecurityLogDTO = new TblSecurityLogDTO();
            BeanUtils.copyProperties(tblSecurityLog, tblSecurityLogDTO);
            tblSecurityLogDTOS.add(tblSecurityLogDTO);
        }

        return success(tblSecurityLogDTOS);

    }


    /**
     * 查询该设备当天数据
     */
    //@CheckLogin
    @GetMapping("/today")
    public R selectToday(@RequestParam("deviceMac") String deviceMac,
                         @RequestParam("gatewayMac") String gatewayMac) {


        List<TblSecurityLog> tblSecurityLogs = tblSecurityLogService.selectByToday(gatewayMac, deviceMac);

        List<TblSecurityLogDTO> tblSecurityLogDTOS = new ArrayList<>();
        for (TblSecurityLog tblSecurityLog : tblSecurityLogs) {
            TblSecurityLogDTO tblSecurityLogDTO = new TblSecurityLogDTO();
            BeanUtils.copyProperties(tblSecurityLog, tblSecurityLogDTO);
            tblSecurityLogDTOS.add(tblSecurityLogDTO);
        }

        return success(tblSecurityLogDTOS);

    }

    /**
     * 查询该设备某天数据
     */
    //@CheckLogin
    @GetMapping("/day")
    public R selectByDay(@RequestParam("deviceMac") String deviceMac,
                         @RequestParam("gatewayMac") String gatewayMac,
                         @RequestParam("time") long time) {

        List<TblSecurityLog> tblSecurityLogs = tblSecurityLogService.selectByDay(gatewayMac, deviceMac, time);

        List<TblSecurityLogDTO> securityLogDto = new ArrayList<>();
        for (TblSecurityLog tblSecurityLog : tblSecurityLogs) {
            TblSecurityLogDTO tblSecurityLogDTO = new TblSecurityLogDTO();
            BeanUtils.copyProperties(tblSecurityLog, tblSecurityLogDTO);
            securityLogDto.add(tblSecurityLogDTO);
        }

        return success(securityLogDto);

    }


    /**
     * 通过时间查询
     *
     * @return 单条数据
     */
    @GetMapping("/count")
    public R selectTimeCount(@RequestParam("deviceMac") String deviceMac,
                             @RequestParam("gatewayMac") String gatewayMac,
                             @RequestParam("start") long start,
                             @RequestParam("end") long end) {


        String startTime = "'" + Time.stampToDate(start) + "'";
        String endTime = "'" + Time.stampToDate(end) + "'";
        List<TimeCount> list = tblSecurityLogService.selectTimeCount(gatewayMac, deviceMac, startTime, endTime);
        List<TimeCountDTO> timeCountDTOS = new ArrayList<>();
        for (TimeCount timeCount : list) {
            //时间字符串转时间戳
            long timestamp = 0;
            try {
                timestamp = new SimpleDateFormat("yyyy-MM").parse(timeCount.getTime()).getTime();
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }
            TimeCountDTO timeCountDTO = new TimeCountDTO();
            timeCountDTO.setCount(timeCount.getCount());
            timeCountDTO.setTime(timestamp);
            timeCountDTOS.add(timeCountDTO);
        }

        return success(timeCountDTOS);

    }

    /**
     * 根据deviceMac删除全部日志
     *
     * @return
     */
    @DeleteMapping("/deleteAll/deviceMac")
    public String deleteAllTblSecurityByDeviceMac(@RequestParam("deviceMac") String deviceMac) {


        int i = this.tblSecurityLogService.deleteAllByDeviceMac(deviceMac);
        if (i > 0) {
            //return success(null);
            return "删除成功";
        } else {
            //return failed("删除失败");
            return "删除失败";
        }

    }

    /**
     * 根据日志id批量删除日志
     *
     * @return
     */
    @DeleteMapping("/delete/ids")
    public String deleteTblSecurityByIds(@RequestBody List<String> ids) {
        int i = this.tblSecurityLogService.deleteByIds(ids);
        if (i > 0) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }


}
